#!/bin/bash

LOCATOR=./src/apps/locator/worker-locator
REGRESSION_TESTER=./src/tests/regression-tester

test_name=

function test_passed
{
    echo -e "\033[40m\033[1;32m ${test_name} ...Test Passed!  \033[0m"
}

function test_failed
{
    echo -e "\033[40m\033[1;31m ${test_name} ...Test Failed!   \033[0m"
}



function check_result
{
    RESULT=$?
    echo
    if [[ $RESULT == "0" ]]
    then
	test_passed
    else
	test_failed
    fi
}

function restart_locator
{
    echo "(re)starting locator"
    
    if [ -e ${HOME}/kibitz-locator.pid ]
    then
	#kill -SIGINT `cat ${HOME}/kibitz-locator.pid`
	echo "locator already running"
    else
	$LOCATOR -d --pid-file=${HOME}/kibitz-locator.pid --graph-definition-file=$(pwd)/config/test1.graph --host=localhost --log-level=debug --log-path=$(pwd)/log/locator.log
    fi


}


rm -fv test/*
rm log/*

restart_locator

echo "Waiting for locator to initialize..."
sleep 3
echo "Test heartbeats."
script/capture-heartbeat.rb "tcp://localhost:5556"

check_result

# echo "Starting workers for traversal test"
# echo "A -> B,C -> D"


KIBITZ_ROLE=ROOT $REGRESSION_TESTER -I1 -TA -Llocalhost --log-level=debug --log-path=$(pwd)/log &
KIBITZ_ROLE=CHILD $REGRESSION_TESTER -I1 -TB -Llocalhost  --log-level=debug --log-path=$(pwd)/log &
KIBITZ_ROLE=CHILD $REGRESSION_TESTER -I1 -TC -Llocalhost --log-level=debug --log-path=$(pwd)/log &
KIBITZ_ROLE=CHILD $REGRESSION_TESTER -I1 -TD -Llocalhost -P9001 --log-level=debug --log-path=$(pwd)/log &

sleep 4


KIBITZ_LOCATOR='tcp://localhost:5557' script/kibitz initiate --worker-type=A --worker-id=1

sleep 4

echo "Test traversal A -> B"
script/validate test/B.1.in test/A.1.out
check_result

# test_name="Traversal A -> C"
script/validate test/C.1.in test/A.1.out
check_result

# test_name="Traversal B -> D"
script/validate test/D.1.in test/B.1.out
check_result

# test_name="Traversal C -> D"
script/validate test/D.1.in test/C.1.out
check_result

# test_name="D should have 2 messages"
script/check_count test/D.1.in 2
check_result


rm -fv test/*


KIBITZ_ROLE=CHILD $REGRESSION_TESTER -I2 -TB -Llocalhost --log-level=debug --log-path=$(pwd)/log &

sleep 4
# first message will go to b.1 
KIBITZ_LOCATOR='tcp://localhost:5557' script/kibitz initiate --worker-type=A --worker-id=1

sleep 3

rm -fv test/* 

# second message should go to b.2
KIBITZ_LOCATOR='tcp://localhost:5557' script/kibitz initiate --worker-type=A --worker-id=1 

sleep 3

script/validate test/B.2.in test/A.1.out
check_result

# test_name="Traversal A -> C"
script/validate test/C.1.in test/A.1.out
check_result

# test_name="Traversal B -> D"
script/validate test/D.1.in test/B.2.out
check_result

# test_name="Traversal C -> D"
script/validate test/D.1.in test/C.1.out
check_result

# test_name="D should have 2 messages"
script/check_count test/D.1.in 2
check_result


#KIBITZ_LOCATOR="tcp://localhost:5557" script/kibitz stop

killall regression-tester


 echo "shutting down locator"
if [ -e ${HOME}/kibitz-locator.pid ]
then
kill -SIGINT `cat ${HOME}/kibitz-locator.pid`
fi

